/*
 * @Author: levi7754 levi7754@163.com
 * @Date: 2025-10-11 18:25:41
 * @LastEditors: levi7754 levi7754@163.com
 * @LastEditTime: 2025-10-12 18:53:52
 * @FilePath: \udp-nature-mall\store\modules\user.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import { groupBy } from "xe-utils"
import { initDict, getUserAuth } from "@/api"
import { tenantId } from "@/utils"

export default {
  state: {
    chat: {},
    template: {
      // 店铺装修模板
      basic: {}, // 基本信息
      home: {
        // 首页模板
        style: {},
        data: [],
      },
      user: {
        // 个人中心模板
        style: {},
        data: [],
      },
    },
    shareInfo: {}, // 全局分享信息
    authInfo: null,
    dataDict: {}, // 数据字典
    languageList: [], // 语言列表
    lang: {} // 当前语言
  },
  getters: {
  },
  mutations: {
    setDict(state, data) {
        const dictList = groupBy(data.records, 'typeCode');
        Object.keys(dictList).forEach(m => {
          state.dataDict[m] = {
            code: m,
            children: dictList[m].map(m => {
              return {
                dictCode: m.itemVo.value,
                dictName: m.itemVo.meaning
              };
            })
          };
        });
        console.log(state.dataDict)
			  uni.setStorageSync('kDictData', state.dataDict)
    }
  },
  actions: {
    
    getDict({commit}) {
      const data = {
        page: { size: 10000, current: 1 },
        search: {
          tenantId,
          appCodeList: ['BASE', "INV", "SAL"]
        }
      }
      initDict(data).then(res => {
        commit("setDict", res)
      })
    },
    getDataAuth() {
      getUserAuth({code: 'ALL_CUSTOMER'}).then(res => {
        const token = uni.getStorageSync("kAuthorizedToken")
        this.queryAllCustomer = res.some(f => f.userId === token.accountId)
      })
    },
    getDictCode(code) {
      if (!this.dataDict[code]) return []
      const list = this.dataDict[code].children.map(dict => {
        return { value: dict.dictCode, text: dict.dictName }
      })
      return list
    }
  },
};